package A1_二叉树;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LC_144前序遍历_非递归 {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val,TreeNode left,TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }
    public static void main(String[] args){
        TreeNode root=new TreeNode(1);
        root.right=new TreeNode(2);
        root.right.left=new TreeNode(3);
        System.out.println(inorderTraversal(root));
    }

    //二叉树前序遍历：中左右
    public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res=new ArrayList<>();
        if(root==null)
            return res;
        Stack<TreeNode> st=new Stack<>();
        st.push(root);
        while(!st.isEmpty()){
            TreeNode node=st.pop();
            res.add(node.val);
            if(node.right!=null)
                st.push(node.right);
            if(node.left!=null)
                st.push(node.left);
        }
        return res;
    }
    //获取二叉树节点个数
    public static int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int nums=size(root.left) + size(root.right) + 1;
        return nums;
    }
}
